package com.promis.party.ui.home.homebanner.answerwall;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.promis.compat.StatusBarCompat;
import com.promis.party.R;
import com.promis.party.helper.ContextHelper;
import com.promis.party.helper.UIHepler;
import com.promis.util.DrawableUtils;

/**
 * Created by zcy on 2018/9/25.
 */

public class MyAnswerActivity extends AppCompatActivity {

    private ContextHelper contextHelper;

    private AnswerWallFragment answerWallFragmentOne;
    private AnswerWallFragment answerWallFragmentTwo;

    private TextView txtvQuestion;
    private TextView txtvAnswer;
    private TextView[] txtvButtons;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarCompat.translucentStatusBar(this);
        StatusBarCompat.blackTextStatusBar(this);
        setContentView(R.layout.activity_my_answer);
        UIHepler.goBack(MyAnswerActivity.this, R.id.activity_my_answer_imgv_back);

        txtvQuestion = findViewById(R.id.activity_my_answer_txtv_questions);
        txtvAnswer = findViewById(R.id.activity_my_answer_txtv_answer);
        viewPager = findViewById(R.id.activity_my_answer_vpgv_container);
        txtvButtons = new TextView[]{
                txtvQuestion, txtvAnswer
        };

        initData();
        initListener();
    }

    private void initData() {
        contextHelper = new ContextHelper(getBaseContext());

        AnswerListFragmentPagerAdapter adapter = new AnswerListFragmentPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        txtvButtons[0].setSelected(true);
        txtvButtons[0].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_white_bottom));
    }

    private void initListener() {

        for (int i = 0; i < txtvButtons.length; i++) {
            final int position = i;
            txtvButtons[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem(position);
                    if(position == 0) {
                        txtvButtons[0].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_white_bottom));
                        txtvButtons[1].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_red_bottom));
                    }else{
                        txtvButtons[1].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_white_bottom));
                        txtvButtons[0].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_red_bottom));
                    }
                }
            });
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                if(position == 0) {
                    txtvButtons[0].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_white_bottom));
                    txtvButtons[1].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_red_bottom));
                }else{
                    txtvButtons[1].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_white_bottom));
                    txtvButtons[0].setBackground(DrawableUtils.getDrawable(getBaseContext(), R.drawable.shape_stroke_red_bottom));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private class AnswerListFragmentPagerAdapter extends FragmentStatePagerAdapter {

        public AnswerListFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                answerWallFragmentOne = AnswerWallFragment.newInstance(0);
                return answerWallFragmentOne;
            } else if (position == 1) {
                answerWallFragmentTwo = AnswerWallFragment.newInstance(1);
                return answerWallFragmentTwo;
            }
            return null;
        }

        @Override
        public int getCount() {
            return txtvButtons.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return txtvButtons[position].getText();
        }
    }
}